package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketGoodsExample;
import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.db.domain.MarketTopicExample;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketTopicMapper;
import com.cskaoyan.market.service.AdminTopicService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminTopicServiceImpl implements AdminTopicService {
    @Override
    public List<MarketTopic> list(String page, String limit, String sort, String order, String title, String subtitle) {
        SqlSession session = MybatisUtil.getSession();
        MarketTopicMapper topicMapper = session.getMapper(MarketTopicMapper.class);
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        if(!StringUtils.isEmpty(title)){
            criteria.andTitleLike("%" + title + "%");
        }
        if(!StringUtils.isEmpty(subtitle)){
            criteria.andSubtitleLike("%" + subtitle + "%");
        }
        marketTopicExample.setOrderByClause(sort+" "+order);
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<MarketTopic> marketTopics=topicMapper.selectByExample(marketTopicExample);
        session.commit();
        session.close();
        return marketTopics;
    }

    @Override
    public int delete(Integer id) {
        SqlSession session= MybatisUtil.getSession();
        MarketTopicMapper topicMapper = session.getMapper(MarketTopicMapper.class);

        int i = topicMapper.deleteByPrimaryKey(id);
        session.commit();
        session.close();
        return i;
    }

    @Override
    public Map<String, Object> read(String id) {
        Map<String,Object> objectMap=new HashMap<>();
        // topic
        SqlSession session = MybatisUtil.getSession();
        MarketTopicMapper topicMapper = session.getMapper(MarketTopicMapper.class);
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        criteria.andIdEqualTo(Integer.valueOf(id));
        List<MarketTopic> marketTopics=topicMapper.selectByExample(marketTopicExample);
        objectMap.put("topic",marketTopics.get(0));

        // goodsList
        MarketGoodsMapper goodsMapper = session.getMapper(MarketGoodsMapper.class);
        MarketGoodsExample marketGoodsExample=new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria1=marketGoodsExample.createCriteria();
        Integer[] goods = marketTopics.get(0).getGoods();
        for (Integer good : goods) {
            criteria1.andIdEqualTo(good);
        }
        List<MarketGoods> marketGoods=goodsMapper.selectByExample(marketGoodsExample);
        objectMap.put("goodsList",marketTopics);

        session.commit();
        session.close();
        return objectMap;
    }

    @Override
    public int create(MarketTopic marketTopic) {
        SqlSession session= MybatisUtil.getSession();
        MarketTopicMapper topicMapper = session.getMapper(MarketTopicMapper.class);

        int i = topicMapper.insert(marketTopic);
        session.commit();
        session.close();
        return i;
    }

    @Override
    public int update(MarketTopic marketTopic) {
        SqlSession session= MybatisUtil.getSession();
        MarketTopicMapper topicMapper = session.getMapper(MarketTopicMapper.class);
        int i = topicMapper.updateByPrimaryKey(marketTopic);
        session.commit();
        session.close();
        return i;
    }
}
